package com.dayuanit.dy15.lesson29.demo1;

public class Test {

    public static final Object lock1 = new Object();
    public static final Object lock2 = new Object();


    public static void main(String[] args) {
        //线程 死锁

        //死锁发生的条件：首先要有多个线程，多把锁。
        //然后 锁中锁
        //最后 锁的顺序不一样。

        new Thread(new Runnable() {
            @Override
            public void run() {
                synchronized (lock1) {
                    System.out.println(Thread.currentThread().getName() + " lock1");
                    synchronized (lock2) {
                        System.out.println(Thread.currentThread().getName() + " lock2");
                    }
                }
            }
        }, "t1").start();

        new Thread(new Runnable() {
            @Override
            public void run() {
                synchronized (lock2) {
                    System.out.println(Thread.currentThread().getName() + " lock2");
                    synchronized (lock1) {
                        System.out.println(Thread.currentThread().getName() + " lock1");
                    }
                }
            }
        }, "t2").start();
    }
}
